import axios from "axios";


export const sendMessage = ({ data = {}, controller, callback }) => {
  const signal = controller.signal;
  let lastResponseText = '';
  const server = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    // baseURL: process.env.VUE_APP_BASE_API,
    // 超时
    timeout: 10000000
  })

  return server.get('http://localhost:8081/kkk?msg=你好', data, {
    signal,
    timeout: 0,
    onDownloadProgress: event => {
      const currentResponseText = event.target.responseText;
      alert("9999")
      // 获取自上次进度事件以来的新数据
      const newData = currentResponseText.substring(lastResponseText.length);
      // 更新上次响应文本的位置
      lastResponseText = currentResponseText;
      if (newData.trim()) { // 确保新数据不只是空白字符
        try {
          const jsonData = JSON.parse(newData);
          if (jsonData.text !== lastdata.text) {
            console.log('New data received:', jsonData);
            callback(jsonData);
          }
        } catch (error) {
          console.error('Error parsing JSON:', error);
        }
      }
    }
  });
};
